/*
 * Manipulate with popup windows
 */
var popup_windows = new Array();
var popup_positions = new Array();
var popup_mouseStartX;
var popup_mouseStartY;
var popup_windowStartX;
var popup_windowStartY;
var popup_dragged;
var popup_handle;
var popup_canResize;
var popup_resizing;
var popup_moving;
var popup_dialogActive;
var popup_messageActive;

function popup_Position(_left, _top, _width, _height) {
	this.left = _left;
	this.top = _top;
	this.width = _width;
	this.height = _height;
}

function popup_Popup(_title, _content, _id, _width, _height) {
	this.setTitle = function(_title) {
		this.title.textContent = _title;
		this.title.appendChild(this.bClose);
	}

	this.setContent = function(_content) {
		if (this.content != null) {
			this.contentArea.removeChild(this.content);
		}
		this.content = _content;
		this.contentArea.appendChild(this.content);
	}

	this.cropSize = function() {
		var width = Math.min(window.innerWidth - 20, this.frame.offsetWidth);
		var height = Math.min(window.innerHeight - 20, this.frame.offsetHeight);
		this.frame.style.width = width + "px";
		this.frame.style.height = height + "px";
		return [ width, height ];
	}

	this.center = function() {
		var wh = this.cropSize();
		this.frame.style.left = (window.innerWidth - wh[0]) / 2 + "px";
		this.frame.style.top = (window.innerHeight - wh[1]) / 2 + "px";
	}

	this.getPosition = function() {
		var left = this.frame.style.left;
		var top = this.frame.style.top;
		var width = this.frame.style.width;
		var height = this.frame.style.height;
		return new popup_Position(left, top, width, height);
	}

	this.place = function() {
		if (this.userPosition) {
			this.frame.style.left = this.position.left;
			this.frame.style.top = this.position.top;
			this.frame.style.width = this.position.width;
			this.frame.style.height = this.position.height;
		} else {
			this.frame.style.width = this.width;
			this.frame.style.height = this.height;
			this.center();
		}
	}

	popup_windows[_id] = this;
	this.frame = document.createElement("div");
	this.frame.setAttribute("class", "popup");
	this.frame.onmousemove = popup_selectHandle;
	this.frame.onmousedown = popup_startResize;
	this.frame.setAttribute("onMouseOut",
			"document.body.style.cursor = 'default';");
	this.frame.popup = this;
	this.title = document.createElement("div");
	this.title.setAttribute("class", "popupTitle");
	this.title.setAttribute("onmousedown", "return false;");
	this.title.onmousedown = popup_titleDown;
	this.bClose = document.createElement("div");
	this.bClose.setAttribute("onClick", "popup_hide('" + _id + "')");
	this.bClose.setAttribute("onmousedown", "return false;");
	this.bClose.textContent = "X";
	this.bClose.className = "button";
	this.bClose.style.position = "absolute";
	this.bClose.style.right = "0.3em";
	this.bClose.style.top = "0.3em";
	this.bClose.title = _T("Close");
	this.frame.appendChild(this.title);
	this.contentArea = document.createElement("div");
	this.contentArea.className = "popupContent";
	this.frame.appendChild(this.contentArea);
	this.setTitle(_title);
	this.setContent(_content);
	this.frame.appendChild(this.contentArea);
	this.position = popup_positions[_id];
	this.width="auto";
	this.height="auto";
	if(_width){
		this.width=_width;
	}
	if(_height){
		this.height=_height;
	}
	this.id=_id;
	this.userPosition = (this.position);
}

function popup_hide(_id) {
	var popup = popup_windows[_id];
	if (popup != null) {
		if (popup.userPosition) {
			popup_positions[_id] = popup.getPosition();
		}
		document.body.removeChild(popup_windows[_id].frame);
		popup_windows[_id] = null;
	}
}

function popup_titleDown(_e) {
	popup_dragged = _e.currentTarget.parentNode;
	popup_windowStartX = popup_dragged.offsetLeft;
	popup_windowStartY = popup_dragged.offsetTop;
	popup_mouseStartX = _e.clientX;
	popup_mouseStartY = _e.clientY;
	document.onmousemove = popup_move;
	document.onmouseup = popup_mouseUp;
	document.body.style.cursor = "move";
	popup_moving = true;
	return false;
}

function popup_mouseUp(_e) {
	document.onmousemove = null;
	document.onmouseup = null;
	popup_resizing = false;
	popup_moving = false;
	document.body.style.cursor = "default";
	return false;
}

function popup_move(_e) {
	popup_dragged.style.left = Math.max(0, popup_windowStartX
			+ (_e.clientX - popup_mouseStartX))
			+ "px";
	popup_dragged.style.top = Math.max(0, popup_windowStartY
			+ (_e.clientY - popup_mouseStartY))
			+ "px";
	popup_dragged.popup.userPosition = true;
	return false;
}

function popup_selectHandle(_e) {
	if (popup_resizing || popup_moving) {
		return;
	}
	// alert(debugObject(_e.target));
	var tolerance = 6;
	popup_canResize = false;
	var tresholdW = _e.currentTarget.offsetWidth - tolerance;
	var tresholdH = _e.currentTarget.offsetHeight - tolerance;
	var x = _e.clientX - _e.currentTarget.offsetLeft;
	var y = _e.clientY - _e.currentTarget.offsetTop;
	if (x > tresholdW && y > tresholdH) {
		popup_canResize = true;
		document.body.style.cursor = "se-resize";

	} else {
		document.body.style.cursor = "default";
	}
}

function popup_startResize(_e) {
	if (popup_canResize) {
		popup_resizing = true;
		popup_dragged = _e.currentTarget;
		var left = popup_dragged.offsetLeft;
		var top = popup_dragged.offsetTop;
		popup_windowStartW = popup_dragged.offsetWidth;
		popup_windowStartH = popup_dragged.offsetHeight;
		popup_mouseStartX = _e.clientX;
		popup_mouseStartY = _e.clientY;
		document.onmousemove = popup_resize;
		document.onmouseup = popup_mouseUp;
		return false;
	}
}

function popup_resize(_e) {
	var changeX = _e.clientX - popup_mouseStartX;
	var changeY = _e.clientY - popup_mouseStartY;
	popup_dragged.style.width = (popup_windowStartW + changeX) + "px";
	popup_dragged.style.height = (popup_windowStartH + changeY) + "px";
	popup_dragged.popup.userPosition = true;
	return false;
}

function popup_show(_title, _content, _id, _width, _height) {
	popup_hide(_id);
	var popup = new popup_Popup(_title, _content, _id, _width, _height);
	document.body.appendChild(popup.frame);
	popup.place();
	return popup;
}

function popup_showWait(_id) {
	var waitDiv = document.createElement("div");
	waitDiv.appendChild(loadingMsg);
	var popup = popup_show(_T("WaitDots"), waitDiv, _id);
	return popup;
}

function popup_showDialog(_title,_content, _yesAction, _noAction, _width, _height) {
	if(popup_dialogActive){
		return;
	}
	popup_dialogActive=true;
	var toolbar = document.createElement("p");
	toolbar.style.textAlign="center";
	var bYes = main_createButton(_T("Continue"),
			"popup_hide('popup_dialogWindow');popup_dialogActive=false;"
					+ _yesAction);
	var bNo = main_createButton(_T("Cancel"),
			"popup_hide('popup_dialogWindow');popup_dialogActive=false;"
					+ _noAction);
	toolbar.appendChild(bYes);
	toolbar.appendChild(bNo);
	var content = document.createElement("div");
	content.appendChild(_content);
	content.appendChild(toolbar);
	var popup = popup_show(_title, content, "popup_dialogWindow", _width, _height);
	return popup;
}

function popup_showMessage(_title,_content, _action, _width, _height) {
	if(popup_messageActive){
		return;
	}
	popup_messageActive=true;
	var toolbar = document.createElement("p");
	toolbar.style.textAlign="center";
	toolbar.appendChild(main_createButton(_T("OK"),"popup_hide('popup_messageWindow');popup_messageActive=false;"
			+_action));
	var content = document.createElement("div");
	content.appendChild(_content);
	content.appendChild(toolbar);
	var popup = popup_show(_title, content, "popup_messageWindow", _width, _height);
	return popup;
}

